﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cspl.AnimalFight.Lib.Interface;
using Cspl.AnimalFight.Lib.EventHandlers;

namespace Cspl.AnimalFight.Lib.Data
{
    public class Dog : AnimalBase, IAttack, IDefend, IAttackable
    {
        private DogMood _dogMood; 
        
        public event AttackDelegate AttackEvent;
        public event DefendDelegate DefendEvent;

        public bool isDefending { get; set; }

        public DogMood Mood
        {
            get { return _dogMood; }
            set { _dogMood = value; }
        }

        public void Defend()
        {
            if (DefendEvent != null)
            {
                DefendEvent(this, new DefendEventHandler 
                {
                    AnimalName = this.Name,
                    Target = Animals.Dog
                });
            }
        }

        public void Attack(string targetName)
        {
            if (this.IsDead)
            {
                Console.WriteLine("{0} is already dead. Cannot attack other animals.", this.Name);
            }
            else if (this.HasEscape)
            {
                Console.WriteLine("{0} has escaped. Cannot attack other animals.", this.Name);
            }
            else
            {
                Console.WriteLine("{0} is attacking {1}.", this.Name, targetName);

                int theRealAttackPower = 0;

                if (this.Mood == DogMood.Bad)
                {
                    theRealAttackPower = (int)(0.5 * this.AP);
                }
                else
                {
                    theRealAttackPower = this.AP;
                }

                if (AttackEvent != null)
                {
                    AttackEventHandler eventHandler = new AttackEventHandler
                    {
                        AnimalTargetName = targetName,
                        AP = theRealAttackPower
                    };

                    AttackEvent(this, eventHandler);
                }
            }
        }

        public void Attacked(int ap)
        {
            if (this.HasEscape)
            {
                Console.WriteLine("The {0} has escaped. Cannot be attacked.", this.Name);
            }
            else if (this.IsDead)
            {
                Console.WriteLine("The {0} is already dead. Cannot be attacked.", this.Name);
            }
            else
            {
                Console.WriteLine("{0} is being attacked. Current HP = {1}", this.Name, this.HP);

                int realDp = this.isDefending ? this.DP + 10 : this.DP;
                realDp = this.Mood == DogMood.Bad ? realDp - 10 : realDp;

                // if the attack is less powerfull than defense, then it will be absorbed
                this.HP -= (ap - realDp);

                Console.WriteLine("{0} has been attacked. -{1}", this.Name, ap - realDp);
                Console.WriteLine("Current HP = {0}", this.HP);

                if (this.HP <= 0)
                {
                    this.IsDead = true;
                    Console.WriteLine("{0} is dead.", this.Name);
                }
            }
        }

        public override void Escape()
        {
            this.HasEscape = true;
            Console.WriteLine("{0} has escaped.", this.Name);
        }
    }

    public enum DogMood { Good, Bad }
}
